package RedisObject;

import Dict.Dict;
import Dict.DictEntry;
import IntSet.IntSet;
import QuickList.QuickList;
import RedisObject.enums.RedisEncoding;
import SDS.SDS;
import SkipList.SkipList;
import SkipList.SkipListNode;
import ZipList.ZipList;
import ZipList.ZipListEntry;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        // 字符串输出
        System.out.println("=======字符串输出========");
        RedisObject stringObj = RedisFactory.createString("hi, string");
        System.out.println(stringObj.getRedisType());
        if (stringObj.getRedisEncoding().equals(RedisEncoding.REDIS_ENCODING_INT)) {
            int value = (int) stringObj.getPtr();
            System.out.println(value);
        }

        else if (stringObj.getRedisEncoding().equals(RedisEncoding.REDIS_ENCODING_RAW)) {
            SDS value = (SDS) stringObj.getPtr();
            System.out.println(value.getAllData());
        }

        else {
            SDS value = (SDS) stringObj.getPtr();
            System.out.println(value.getAllData());
        }

        // 列表输出
        System.out.println("=======列表输出========");
        RedisObject listObj = RedisFactory.createList(Arrays.asList("hi", "list"));
        System.out.println(listObj.getRedisType());
        if (listObj.getRedisEncoding().equals(RedisEncoding.REDIS_ENCODING_ZIPLIST)) {
            ZipList zipList = (ZipList) listObj.getPtr();
            ZipListEntry[] allData = zipList.getAllData();
            for (ZipListEntry entry : allData) {
                System.out.println(new String(entry.getData(), StandardCharsets.UTF_8));
            }
        }

        else {
            QuickList quickList = (QuickList) listObj.getPtr();
            ZipListEntry[] allData = quickList.getAllData();
            for (ZipListEntry entry : allData) {
                System.out.println(new String(entry.getData()));
            }
        }

        // 集合输出
        System.out.println("=======集合输出========");
        RedisObject setObj = RedisFactory.createSet(Arrays.asList("hi", "set"));
        System.out.println(setObj.getRedisType());
        if (setObj.getRedisEncoding().equals(RedisEncoding.REDIS_ENCODING_INTSET)) {
            IntSet intSet = (IntSet) setObj.getPtr();
            Object allData = intSet.getAllData();
            System.out.println(allData);
        }

        else {
            Dict dict = (Dict) setObj.getPtr();
            DictEntry[] allData = dict.getAllData();
            for (DictEntry entry : allData) {
                System.out.println(entry.getKey());
            }
        }

        // 有序集合输出
        System.out.println("=======有序集合输出========");
        Map<Object, Double> paramsMap = new HashMap<>();
        paramsMap.put("hi", 1.0);
        paramsMap.put("zset", 3.0);
        RedisObject zsetObj = RedisFactory.createZSet(paramsMap);
        System.out.println(zsetObj.getRedisType());
        if (zsetObj.getRedisEncoding().equals(RedisEncoding.REDIS_ENCODING_SKIPLIST)) {
            SkipList skipList = (SkipList) zsetObj.getPtr();
            SkipListNode[] allData = skipList.getAllData();
            for (SkipListNode node : allData) {
                System.out.println("Node value: " + node.getValue() + ", Node score: " + node.getScore());
            }
        }

        else {
            ZipList zipList = (ZipList) zsetObj.getPtr();
            ZipListEntry[] allData = zipList.getAllData();
            for (ZipListEntry data : allData) {
                System.out.println("Data: " + new String(data.getData()));
            }
        }

        // 哈希输出
        System.out.println("=======哈希输出========");
        Map<Object, Object> paramsMap2 = new HashMap<>();
        paramsMap2.put("name", "hash");
        paramsMap2.put("age", 24);
        RedisObject hashObj = RedisFactory.createHash(paramsMap2);
        System.out.println(hashObj.getRedisType());
        if (hashObj.getRedisEncoding().equals(RedisEncoding.REDIS_ENCODING_ZIPLIST)) {
            ZipList zipList = (ZipList) hashObj.getPtr();
            ZipListEntry[] allData = zipList.getAllData();
            for (ZipListEntry data : allData) {
                System.out.println("Data: " + new String(data.getData()));
            }
        }

        else {
            Dict dict = (Dict) hashObj.getPtr();
            DictEntry[] allData = dict.getAllData();
            for (DictEntry entry : allData) {
                System.out.println(entry.getKey() + ": " + entry.getValue());
            }
        }
    }
}
